package com.bst.web.etl;

import java.util.List;

import com.bst.base.service.IBaseLabelService;
import com.bst.etl.domain.EtlDrawBaseDatax;
import com.bst.etl.vo.EtlLabelDataVO;
import com.bst.system.domain.BaseLabel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.bst.common.annotation.Log;
import com.bst.common.enums.BusinessType;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.etl.service.IEtlDrawDwService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 标准仓库Controller
 * 
 * @author ruoyi
 * @date 2024-02-28
 */
@Controller
@RequestMapping("/base/etl/dw")
public class EtlDrawDwController extends BaseController
{
    private String prefix = "etl/dw";

    @Autowired
    private IEtlDrawDwService etlDrawDwService;
    @Autowired
    private IBaseLabelService baseLabelService;

    @GetMapping()
    public String dw(ModelMap mmap)
    {
        BaseLabel label = new BaseLabel();
        label.setParentId(0L);
        label.setSdCls("dw");
        List<BaseLabel> labels = baseLabelService.selectBaseLabelList(label);
        List<BaseLabel> children;
        for (BaseLabel l : labels) {
            label.setParentId(l.getIdLabel());
            children = baseLabelService.selectBaseLabelList(label);
            l.setChildren(children);
        }
        mmap.put("labels",labels);
        return prefix + "/dw";
    }

    /**
     * 查询标准仓库列表
     */
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(EtlLabelDataVO dataVO)
    {
        startPage();
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwList2(dataVO);
        return getDataTable(list);
    }

    /**
     * 导出标准仓库列表
     */
    @Log(title = "标准仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EtlDrawDw etlDrawDw)
    {
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwList(etlDrawDw);
        ExcelUtil<EtlDrawDw> util = new ExcelUtil<EtlDrawDw>(EtlDrawDw.class);
        return util.exportExcel(list, "标准仓库数据");
    }

    /**
     * 新增标准仓库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存标准仓库
     */
    @Log(title = "标准仓库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EtlDrawDw etlDrawDw)
    {
        return toAjax(etlDrawDwService.insertEtlDrawDw(etlDrawDw));
    }

    /**
     * 修改标准仓库
     */
    @GetMapping("/edit/{idDw}")
    public String edit(@PathVariable("idDw") Long idDw, ModelMap mmap)
    {
        EtlDrawDw etlDrawDw = etlDrawDwService.selectEtlDrawDwByIdDw(idDw);
        mmap.put("etlDrawDw", etlDrawDw);
        return prefix + "/edit";
    }

    /**
     * 修改保存标准仓库
     */
    @Log(title = "标准仓库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EtlDrawDw etlDrawDw)
    {
        return toAjax(etlDrawDwService.updateEtlDrawDw(etlDrawDw));
    }

    /**
     * 删除标准仓库
     */
    @RequiresPermissions("etl:dw:remove")
    @Log(title = "标准仓库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(etlDrawDwService.deleteEtlDrawDwByIdDws(ids));
    }

    @GetMapping("/selectByTemplateId")
    @ResponseBody
    public TableDataInfo selectByTemplateId (Integer templateId) {
        List<EtlDrawDw> list = etlDrawDwService.selectEtlDrawDwByIdTemplate(templateId);
        return getDataTable(list);
    }

    @GetMapping("/createTable")
    @Log(title = "建表", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult createTable (Long idDw) {
        AjaxResult result = etlDrawDwService.createDwTable(idDw);
        return result;
    }

    @PostMapping("/dwSQlValid")
    @Log(title = "建表", businessType = BusinessType.QUERY)
    @ResponseBody
    public AjaxResult dwSQlValid (@RequestBody EtlDrawDw etlDrawDw) {
        return etlDrawDwService.dwSQlValid(etlDrawDw.getSqlQry());
    }

}
